#include "delge_squirrelgraphics_model.h"

void s_Graphics_Model_RegisterFunctions (HSQUIRRELVM v)
{
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Model_Create,			_SC("Model_Create"),		_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Model_Destroy,			_SC("Model_Destroy"),		_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Model_SetMesh,			_SC("Model_SetMesh"),		_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Model_SetTexture,		_SC("Model_SetTexture"),	_SC("*"));
}

SQInteger s_Graphics_Model_Create (HSQUIRRELVM v)
{
	const SQChar			*sModelName;
	const SQChar			*sMeshName;
	const SQChar			*sTextureName;

	sq_getstring			(v, 2, &sModelName);
	sq_getstring			(v, 3, &sMeshName);
	sq_getstring			(v, 4, &sTextureName);

	Model *pModel = new Model();

	Mesh *pMesh = NULL;
	Texture *pTexture = NULL;
	
	if (sMeshName != NULL)
		ResourceManager<Mesh>::GetResource(sMeshName);
	if (sTextureName != NULL)
		ResourceManager<Texture>::GetResource(sTextureName);

	pModel->SetMesh(pMesh);
	pModel->SetTexture(pTexture);

	ResourceManager<Model>::LoadResource(string(sModelName), pModel);

	return 0;
}

SQInteger s_Graphics_Model_Destroy (HSQUIRRELVM v)
{
	const SQChar			*sModelName;

	sq_getstring			(v, 2, &sModelName);

	ResourceManager<Model>::UnloadResource(string(sModelName));

	return 0;
}

SQInteger s_Graphics_Model_SetMesh (HSQUIRRELVM v)
{
	const SQChar			*sModelName;
	const SQChar			*sMeshName;

	sq_getstring			(v, 2, &sModelName);
	sq_getstring			(v, 3, &sMeshName);

	Model *pModel = ResourceManager<Model>::GetResource(sModelName);
	Mesh *pMesh = ResourceManager<Mesh>::GetResource(sMeshName);

	pModel->SetMesh(pMesh);

	return 0;
}

SQInteger s_Graphics_Model_SetTexture (HSQUIRRELVM v)
{
	const SQChar			*sModelName;
	const SQChar			*sTextureName;

	sq_getstring			(v, 2, &sModelName);
	sq_getstring			(v, 3, &sTextureName);

	Model *pModel = ResourceManager<Model>::GetResource(sModelName);
	Texture *pTexture = ResourceManager<Texture>::GetResource(sTextureName);

	pModel->SetTexture(pTexture);

	return 0;
}